package com.adayo.memoryfree.base;

import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Process;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import com.adayo.memoryfree.LocalApplication;
import com.adayo.memoryfree.permission.PermissionListener;
import com.adayo.memoryfree.permission.PermissionManager;
import com.adayo.memoryfree.utils.JLogKit;
import com.adayo.memoryfree.utils.JToastKit;

import org.xutils.x;


/**
 * Activity super class
 *
 * @author
 */
public abstract class ActivityBase extends AppCompatActivity {
	public static final int REQUEST_PERMISSION_CODE = 1;
	protected Context context;
	protected PermissionManager helper;
	protected String[] permissions;
	protected Bundle savedInstanceState;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(getLayoutId());
		this.savedInstanceState = savedInstanceState;
		x.view().inject(this);
		context = this;
		LocalApplication.sActivityStack.pushActivity(this);
		permissions = initPermissions();
		if (checkPermissionIsGranted()) {
			initParams(savedInstanceState);
		} else {
			ActivityCompat.requestPermissions(this, permissions, REQUEST_PERMISSION_CODE);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		LocalApplication.sActivityStack.popActivity(this);
		JToastKit.getInstance().cancelCurrentToast();
	}

	protected abstract int getLayoutId();

	protected abstract void initParams(Bundle savedInstanceState);

	protected abstract String[] initPermissions();

	public void requestRuntimePermission(String[] permissions,
										 PermissionListener listener) {
		helper = PermissionManager.with(this);
		//添加权限请求码
		helper.addRequestCode(REQUEST_PERMISSION_CODE);
		//设置权限，可以添加多个权限
		helper.permissions(permissions);
		//设置权限监听器
		helper.setPermissionsListener(listener);
		//请求权限
		helper.request();
	}

	@Override
	public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
		if (requestCode == REQUEST_PERMISSION_CODE) {
			for (String str : permissions) {
				JLogKit.getInstance().i("permission = " + str);
			}
			for (int result : grantResults) {
				JLogKit.getInstance().i("result = " + result);
			}
			if (permissionIsGranted(grantResults)) {
				initParams(savedInstanceState);
			} else {
				JToastKit.getInstance().showMessage("权限被拒绝，关闭程序");
				finish();
			}
		}
//		switch (requestCode) {
//			case REQUEST_PERMISSION_CODE:
//				helper.onPermissionResult(permissions, grantResults);
//				break;
//		}
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
	}

	private boolean permissionIsGranted(int[] grantResults) {
		for (int grant : grantResults) {
			if (grant != PackageManager.PERMISSION_GRANTED) {
				return false;
			}
		}
		return true;
	}

	private boolean checkPermissionIsGranted() {
		if (permissions == null || permissions.length == 0)
			return true;
		for (String permission : permissions) {
			if (ContextCompat.checkSelfPermission(context, permission)
					!= PackageManager.PERMISSION_GRANTED) {
				return false;
			}
		}
		return true;
	}

	protected void startActivity(Class<?> cls, Intent intent) {
		if (intent == null) {
			intent = new Intent(context, cls);
		}
		startActivity(intent);
	}

	protected void startActivityForResult(Class<?> cls, int requestCode) {
		Intent intent = new Intent(context, cls);
		startActivityForResult(intent, requestCode);
	}
}
